package com.curso.lab1.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

@SuppressWarnings("deprecation")
public class SessionFactoryUtil {
	
    private static SessionFactory sessionFactory;
    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.out.println("Initial SessionFactory creation failed. " + ex);
            ex.printStackTrace();
            System.exit(0);
        }
    }
    
    private SessionFactoryUtil() { } // Private constructor
    
    public static SessionFactory getInstance() {
    	return sessionFactory;
    }
    
    public static Session openSession() {
    	return sessionFactory.openSession();
    }

	public static Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	public static void close() {
		if (sessionFactory != null)
			sessionFactory.close();
		sessionFactory = null;
	}
}



